var t = function(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}(require("../../utils/config.js")), e = require("../../templates/modal-view/modal-view.js"), a = require("../../utils/api.js"), o = require("../../utils/util.js"), i = require("../../utils/auth.js"), n = require("../../wxParse/wxParse.js"), s = (require("../../utils/prism.js"), 
require("../../utils/wxApi.js"), require("../../utils/wxRequest.js")), l = getApp(), d = !1, r = t.default.getPageCount;

Page({
    data: {
        title: "文章内容",
        detail: {},
        commentsList: [],
        ChildrenCommentsList: [],
        commentCount: "",
        detailDate: "",
        commentValue: "",
        wxParseData: {},
        display: "none",
        page: 1,
        isLastPage: !1,
        parentID: "0",
        focus: !1,
        placeholder: "写个评论...",
        postID: null,
        scrollHeight: 0,
        postList: [],
        link: "",
        dialog: {
            title: "",
            content: "",
            hidden: !0
        },
        content: "",
        isShow: !1,
        isLoad: !0,
        menuBackgroup: !1,
        likeImag: "like.svg",
        likeList: [],
        likeCount: 0,
        displayLike: "none",
        replayTemplateId: t.default.getReplayTemplateId,
        userid: "",
        toFromId: "",
        commentdate: "",
        flag: 1,
        logo: t.default.getLogo,
        enableComment: !0,
        isLoading: !1,
        total_comments: 0,
        userInfo: l.globalData.userInfo,
        isLoginPopup: !1
    },
    onLoad: function(t) {
        this.getEnableComment(), this.fetchDetailData(t.id), new e.ModalView();
    },
    onShow: function(t) {},
    showLikeImg: function() {
        for (var t = this, e = t.data.detail.avatarurls, a = [], o = 0; o < e.length; o++) {
            var i = "../../images/gravatar.png";
            -1 != e[o].avatarurl.indexOf("wx.qlogo.cn") && (i = e[o].avatarurl), a[o] = i;
        }
        t.setData({
            likeList: a
        });
    },
    onReachBottom: function() {
        var t = this;
        t.data.isLastPage ? console.log("评论已经是最后一页了") : (console.log("当前页" + t.data.page), 
        t.fetchCommentData(), t.setData({
            page: t.data.page + 1
        }));
    },
    onShareAppMessage: function(e) {
        return this.ShowHideMenu(), console.log(e), {
            title: '分享"' + t.default.getWebsiteName + '"的文章：' + this.data.detail.title.rendered,
            path: "pages/detail/detail?id=" + this.data.detail.id,
            imageUrl: this.data.detail.post_thumbnail_image,
            success: function(t) {
                console.log(t);
            },
            fail: function(t) {
                console.log(t);
            }
        };
    },
    saveImgToPhotosAlbumTap: function() {
        wx.downloadFile({
            url: this.data.detail.post_thumbnail_image,
            success: function(t) {
                console.log(t), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        console.log(t), wx.showToast({
                            title: "Oppss!下载成功",
                            icon: "success",
                            duration: 900,
                            success: function() {}
                        });
                    },
                    fail: function(t) {
                        console.log(t), console.log("fail"), wx.showToast({
                            title: "Aho!下载失败...",
                            mask: !0,
                            duration: 2e3
                        });
                    }
                });
            },
            fail: function() {
                console.log("fail"), wx.showToast({
                    title: "Aho!下载失败...",
                    mask: !0,
                    duration: 2e3
                });
            }
        });
    },
    gotowebpage: function() {
        var e = this;
        e.ShowHideMenu();
        var a = "";
        "0" == t.default.getMinAppType ? (a = "../webpage/webpage", wx.navigateTo({
            url: a + "?url=" + e.data.link
        })) : e.copyLink(e.data.link);
    },
    copyLink: function(t) {
        wx.setClipboardData({
            data: t,
            success: function(t) {
                wx.getClipboardData({
                    success: function(t) {
                        wx.showToast({
                            title: "链接已复制",
                            image: "../../images/link.svg",
                            duration: 2e3
                        });
                    }
                });
            }
        });
    },
    clickLike: function(t) {
        var e = this;
        if ("likebottom" == t.target.id && this.ShowHideMenu(), l.globalData.isGetOpenid) {
            var o = {
                openid: l.globalData.openid,
                postid: e.data.postID
            }, i = a.postLikeUrl();
            s.postRequest(i, o).then(function(t) {
                if ("200" == t.data.status) {
                    var a = [], o = l.globalData.userInfo.avatarUrl;
                    a.push(o);
                    var i = a.concat(e.data.likeList), n = parseInt(e.data.likeCount) + 1;
                    e.setData({
                        likeList: i,
                        likeCount: n,
                        displayLike: "block"
                    }), wx.showToast({
                        title: "谢谢点赞",
                        icon: "success",
                        duration: 900,
                        success: function() {}
                    });
                } else "501" == t.data.status ? (console.log(t.data.message), wx.showToast({
                    title: "谢谢，已赞过",
                    icon: "success",
                    duration: 900,
                    success: function() {}
                })) : console.log(t.data.message);
                e.setData({
                    likeImag: "like-on.svg"
                });
            });
        } else e.userAuthorization("1");
    },
    getIslike: function() {
        var t = this;
        if (l.globalData.isGetOpenid) {
            var e = {
                openid: l.globalData.openid,
                postid: t.data.postID
            }, o = a.postIsLikeUrl();
            s.postRequest(o, e).then(function(e) {
                "200" == e.data.status && (t.setData({
                    likeImag: "like-on.svg"
                }), console.log("已赞过"));
            });
        }
    },
    goHome: function() {
        wx.switchTab({
            url: "../index/index"
        });
    },
    praise: function() {
        this.ShowHideMenu();
        var e = this;
        if ("0" == t.default.getMinAppType) l.globalData.isGetOpenid ? wx.navigateTo({
            url: "../pay/pay?flag=1&openid=" + l.globalData.openid + "&postid=" + e.data.postID
        }) : e.userAuthorization("1"); else {
            var a = t.default.getZanImageUrl;
            wx.previewImage({
                urls: [ a ]
            });
        }
    },
    getEnableComment: function(t) {
        var e = this;
        s.getRequest(a.getEnableComment()).then(function(t) {
            null != t.data.enableComment && "" != t.data.enableComment && ("1" === t.data.enableComment ? e.setData({
                enableComment: !0
            }) : e.setData({
                enableComment: !1
            }));
        });
    },
    fetchDetailData: function(t) {
        var e, i = this, d = "none";
        s.getRequest(a.getPostByID(t)).then(function(a) {
            e = a, n.wxParse("article", "html", a.data.content.rendered.replace(/<pre[^>]*><code class=\"([^\s]*)\">/g, '<pre class="pure-highlightjs $1">').replace(/<\/code><\/pre>/g, "</pre>"), i, 5), 
            null != a.data.total_comments && "" != a.data.total_comments && i.setData({
                commentCount: "有" + a.data.total_comments + "条评论"
            });
            var s = a.data.like_count;
            "0" != a.data.like_count && (d = "block"), i.setData({
                detail: a.data,
                likeCount: s,
                postID: t,
                link: a.data.link,
                detailDate: o.cutstr(a.data.date, 10, 1),
                display: "block",
                displayLike: d,
                total_comments: a.data.total_comments
            });
            var l = wx.getStorageSync("readLogs") || [];
            l.length > 0 && (l = l.filter(function(e) {
                return e[0] !== t;
            })), l.length > 19 && l.pop(), l.unshift([ t, a.data.title.rendered ]), wx.setStorageSync("readLogs", l);
        }).then(function(t) {
            wx.setNavigationBarTitle({
                title: e.data.title.rendered
            });
        }).then(function(o) {
            var n = [];
            n = e.data.tags;
            for (var l = "", d = 0; d < n.length; d++) l += 0 == d ? n[d] : "," + n[d];
            "" != l && s.getRequest(a.getPostsByTags(t, l)).then(function(t) {
                i.setData({
                    postList: t.data
                });
            });
        }).then(function(t) {}).then(function(t) {
            i.showLikeImg();
        }).then(function(t) {}).then(function(t) {
            l.globalData.isGetOpenid || i.userAuthorization("0");
        }).then(function(t) {
            l.globalData.isGetOpenid && i.getIslike();
        }).catch(function(t) {});
    },
    wxParseTagATap: function(e) {
        var i = this, n = e.currentTarget.dataset.src;
        console.log(n);
        var l = t.default.getDomain;
        if (-1 == n.indexOf(l)) wx.setClipboardData({
            data: n,
            success: function(t) {
                wx.getClipboardData({
                    success: function(t) {
                        wx.showToast({
                            title: "链接已复制",
                            image: "../../images/link.png",
                            duration: 2e3
                        });
                    }
                });
            }
        }); else {
            var d = o.GetUrlFileName(n, l);
            "index" == d ? wx.switchTab({
                url: "../index/index"
            }) : s.getRequest(a.getPostBySlug(d)).then(function(e) {
                if (200 == e.statusCode) if (0 != e.data.length) {
                    var a = e.data[0].id, o = wx.getStorageSync("openLinkCount") || 0;
                    o > 4 ? wx.redirectTo({
                        url: "../detail/detail?id=" + a
                    }) : (wx.navigateTo({
                        url: "../detail/detail?id=" + a
                    }), o++, wx.setStorageSync("openLinkCount", o));
                } else {
                    var s = "../webpage/webpage";
                    "0" == t.default.getMinAppType ? (s = "../webpage/webpage", wx.navigateTo({
                        url: s + "?url=" + n
                    })) : i.copyLink(n);
                }
            }).catch(function(t) {
                console.log(response.data.message);
            });
        }
    },
    fetchCommentData: function() {
        var t = this, e = {};
        e.postId = t.data.postID, e.limit = r, e.page = t.data.page, t.setData({
            isLoading: !0
        }), s.getRequest(a.getCommentsReplay(e)).then(function(e) {
            200 == e.statusCode && (e.data.data.length < r && t.setData({
                isLastPage: !0
            }), e.data && t.setData({
                commentsList: [].concat(t.data.commentsList, e.data.data)
            }));
        }).then(function(t) {}).catch(function(t) {
            console.log(t.data.message);
        }).finally(function() {
            t.setData({
                isLoading: !1
            });
        });
    },
    fetchChildrenCommentData: function(t, e) {
        var i = this;
        s.getRequest(a.getChildrenComments(t)).then(function(t) {
            t.data && i.setData({
                ChildrenCommentsList: i.data.ChildrenCommentsList.concat(t.data.map(function(t) {
                    var e = o.removeHTML(t.content.rendered), a = t.date;
                    return t.summary = e, t.date = o.formatDateTime(a), -1 != t.author_url.indexOf("wx.qlogo.cn") ? -1 == t.author_url.indexOf("https") && (t.author_url = t.author_url.replace("http", "https")) : t.author_url = "../../images/gravatar.png", 
                    t;
                }))
            }), setTimeout(function() {
                "1" == e && i.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "评论已提交成功，请等待管理员审核。审核通过后会显示在评论列表内。"
                });
            }, 900);
        });
    },
    ShowHideMenu: function() {
        this.setData({
            isShow: !this.data.isShow,
            isLoad: !1,
            menuBackgroup: !this.data.false
        });
    },
    hiddenMenubox: function() {
        this.setData({
            isShow: !1,
            menuBackgroup: !1
        });
    },
    loadMore: function(t) {
        var e = this;
        e.data.isLastPage ? wx.showToast({
            title: "没有更多内容",
            mask: !1,
            duration: 1e3
        }) : (e.setData({
            page: e.data.page + 1
        }), console.log("当前页" + e.data.page), this.fetchCommentData());
    },
    replay: function(t) {
        var e = this, a = t.target.dataset.id, o = t.target.dataset.name, i = t.target.dataset.userid, n = t.target.dataset.formid, s = t.target.dataset.commentdate;
        d = !0, "1" == e.data.enableComment && e.setData({
            parentID: a,
            placeholder: "回复" + o + ":",
            focus: !0,
            userid: i,
            toFromId: n,
            commentdate: s
        }), console.log("toFromId", n), console.log("replay", d);
    },
    onReplyBlur: function(t) {
        var e = this;
        console.log("onReplyBlur", d), d || "" === t.detail.value.trim() && e.setData({
            parentID: "0",
            placeholder: "写个评论...",
            userid: "",
            toFromId: "",
            commentdate: ""
        }), console.log(d);
    },
    onRepleyFocus: function(t) {
        var e = this;
        d = !1, console.log("onRepleyFocus", d), e.data.focus || e.setData({
            focus: !0
        });
    },
    formSubmit: function(t) {
        var e = this, i = t.detail.value.inputComment, n = e.data.parentID, d = t.detail.value.inputPostID, r = t.detail.formId, c = e.data.userid, u = e.data.toFromId, g = e.data.commentdate;
        if (0 === i.length) e.setData({
            "dialog.hidden": !1,
            "dialog.title": "提示",
            "dialog.content": "没有填写评论内容。"
        }); else if (l.globalData.isGetOpenid) {
            var m = l.globalData.userInfo.nickName, p = l.globalData.userInfo.avatarUrl, f = l.globalData.openid + "@qq.com", h = l.globalData.openid, w = l.globalData.userInfo.nickName, D = {
                post: d,
                author_name: m,
                author_email: f,
                content: i,
                author_url: p,
                parent: n,
                openid: h,
                userid: c,
                formId: r
            }, v = a.postWeixinComment();
            s.postRequest(v, D).then(function(t) {
                if (200 == t.statusCode) if ("200" == t.data.status) {
                    if (e.setData({
                        content: "",
                        parentID: "0",
                        userid: 0,
                        placeholder: "写个评论...",
                        focus: !1,
                        commentsList: []
                    }), console.log(t.data.message), "0" != n && !o.getDateOut(g) && "" != u) {
                        var l = {
                            openid: t.data.useropenid,
                            postid: d,
                            template_id: e.data.replayTemplateId,
                            form_id: u,
                            total_fee: i,
                            fromUser: w,
                            flag: 3,
                            parent: n
                        };
                        v = a.sendMessagesUrl(), s.postRequest(v, l).then(function(t) {
                            t.data.status, console.log(t.data.message);
                        });
                    }
                    console.log(t.data.code);
                    var r = parseInt(e.data.total_comments) + 1;
                    e.setData({
                        total_comments: r,
                        commentCount: "有" + r + "条评论"
                    });
                } else "500" == t.data.status && e.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "评论失败，请稍后重试。"
                }); else "rest_comment_login_required" == t.data.code ? e.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "需要开启在WordPress rest api 的匿名评论功能！"
                }) : "rest_invalid_param" == t.data.code && t.data.message.indexOf("author_email") > 0 ? e.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "email填写错误！"
                }) : (console.log(t.data.code), e.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "评论失败," + t.data.message
                }));
            }).then(function(t) {
                e.setData({
                    page: 1,
                    commentsList: [],
                    isLastPage: !1
                }), e.onReachBottom(), setTimeout(function() {
                    e.setData({
                        "dialog.hidden": !1,
                        "dialog.title": "温馨提示",
                        "dialog.content": "评论已提交成功，请等待管理员审核。文明评论，请勿灌水或者重复提交评论噢~"
                    });
                }, 900);
            }).catch(function(t) {
                console.log(t), e.setData({
                    "dialog.hidden": !1,
                    "dialog.title": "提示",
                    "dialog.content": "评论失败," + t
                });
            });
        } else e.userAuthorization("1");
    },
    userAuthorization: function(t) {
        var e = this;
        wx.getSetting({
            success: function(a) {
                console.log(a.authSetting);
                var o = a.authSetting;
                "scope.userInfo" in o ? (console.log("不是第一次授权", o), !1 === o["scope.userInfo"] ? "1" == t && wx.showModal({
                    title: "用户未授权",
                    content: "如需正常使用评论、点赞、支持等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?",
                    showCancel: !0,
                    cancelColor: "#296fd0",
                    confirmColor: "#296fd0",
                    confirmText: "设置权限",
                    success: function(t) {
                        t.confirm && (console.log("用户点击确定"), wx.openSetting({
                            success: function(t) {
                                console.log("打开设置", t.authSetting), t.authSetting["scope.userInfo"] && i.getUsreInfo(null);
                            }
                        }));
                    }
                }) : i.getUsreInfo(null)) : (console.log("第一次授权"), "1" == t && e.setData({
                    isLoginPopup: !0
                }));
            }
        });
    },
    agreeGetUser: function(t) {
        var e = t.detail.userInfo, a = this;
        e && (i.getUsreInfo(t.detail), a.setData({
            userInfo: e
        })), setTimeout(function() {
            a.setData({
                isLoginPopup: !1
            });
        }, 1200);
    },
    closeLoginPopup: function() {
        this.setData({
            isLoginPopup: !1
        });
    },
    openLoginPopup: function() {
        this.setData({
            isLoginPopup: !0
        });
    },
    confirm: function() {
        this.setData({
            "dialog.hidden": !0,
            "dialog.title": "",
            "dialog.content": ""
        });
    },
    downimageTolocal: function() {
        var e = this;
        e.ShowHideMenu();
        var i = e.data.detail.id, n = e.data.detail.title.rendered, d = "pages/detail/detail?id=" + i, r = o.removeHTML(e.data.detail.excerpt.rendered), c = "", u = "", g = "", m = !1, p = !1, f = (t.default.getDomain, 
        t.default.getDownloadFileDomain), h = e.data.detail.content_first_image;
        if (h) {
            for (var w = 0, D = 0; D < f.length; D++) if (-1 != h.indexOf(f[D].domain)) {
                w++;
                break;
            }
            w > 0 ? (p = !1, c = h) : (c = t.default.getPostImageUrl, u = c, p = !0);
        } else c = t.default.getPostImageUrl, u = c, p = !0;
        if (console.log(c), l.globalData.isGetOpenid) {
            var v = {
                postid: i,
                path: d,
                openid: l.globalData.openid
            }, x = a.creatPoster(), I = a.getPosterQrcodeUrl() + "qrcode-" + i + ".png";
            s.postRequest(x, v).then(function(t) {
                return 200 != t.statusCode ? (console.log(t), m = !1, wx.showToast({
                    title: "生成海报失败...",
                    mask: !0,
                    duration: 2e3
                }), !1) : "200" != t.data.status ? (console.log(t), m = !1, wx.showToast({
                    title: "生成海报失败...",
                    mask: !0,
                    duration: 2e3
                }), !1) : void wx.downloadFile({
                    url: I,
                    success: function(t) {
                        if (200 !== t.statusCode) return console.log(t), m = !1, wx.showToast({
                            title: "生成海报失败...",
                            mask: !0,
                            duration: 2e3
                        }), !1;
                        g = t.tempFilePath, console.log("二维码图片本地位置：" + t.tempFilePath), p ? u && g && e.createPosterLocal(u, g, n, r) : wx.downloadFile({
                            url: c,
                            success: function(t) {
                                if (200 !== t.statusCode) return console.log(t), wx.hideLoading(), wx.showToast({
                                    title: "生成海报失败...",
                                    mask: !0,
                                    duration: 2e3
                                }), !1;
                                u = t.tempFilePath, console.log("文章图片本地位置：" + t.tempFilePath), m = !0, u && g && e.createPosterLocal(u, g, n, r);
                            }
                        }).onProgressUpdate(function(t) {
                            console.log("下载文章图片进度：" + t.progress);
                        });
                    }
                }).onProgressUpdate(function(t) {
                    console.log("下载二维码进度", t.progress);
                });
            });
        }
    },
    createPosterLocal: function(t, e, a, i) {
        var n = this;
        wx.showLoading({
            title: "正在生成海报",
            mask: !0
        });
        var s = wx.createCanvasContext("mycanvas");
        s.setFillStyle("#ffffff"), s.fillRect(0, 0, 600, 970), s.drawImage(t, 0, 0, 600, 400), 
        s.drawImage(e, 210, 720, 180, 180), s.setFillStyle("#000000"), s.setFontSize(20), 
        s.setTextAlign("center"), s.fillText("阅读文章,请长按识别二维码", 300, 940), s.setFillStyle("#000000"), 
        s.beginPath(), s.moveTo(30, 690), s.lineTo(570, 690), s.stroke(), o.drawTitleExcerpt(s, a, i), 
        s.draw(), setTimeout(function() {
            wx.canvasToTempFilePath({
                canvasId: "mycanvas",
                success: function(t) {
                    t.tempFilePath;
                    wx.hideLoading(), console.log("海报图片路径：" + t.tempFilePath), n.modalView.showModal({
                        title: "保存至相册可以分享到朋友圈",
                        confirmation: !1,
                        confirmationText: "",
                        inputFields: [ {
                            fieldName: "posterImage",
                            fieldType: "Image",
                            fieldPlaceHolder: "",
                            fieldDatasource: t.tempFilePath,
                            isRequired: !1
                        } ],
                        confirm: function(t) {
                            console.log(t);
                        }
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }, 900);
    },
    creatPoster: function() {
        var t = this;
        if (t.ShowHideMenu(), t.data.posterImageUrl) return r = "../poster/poster?posterImageUrl=" + c, 
        wx.navigateTo({
            url: r
        }), !0;
        var e = t.data.detail.id, o = t.data.detail.title.rendered, i = "pages/detail/detail?id=" + e, n = "";
        if (t.data.detail.content_first_image && (n = t.data.detail.content_first_image), 
        wx.showLoading({
            title: "正在生成图片",
            mask: !1
        }), l.globalData.isGetOpenid) {
            var d = {
                postid: e,
                title: o,
                path: i,
                postImageUrl: n,
                openid: l.globalData.openid
            }, r = a.creatPoster(), c = a.getPosterUrl() + "poster-" + e + ".jpg";
            s.postRequest(r, d).then(function(t) {
                200 == t.statusCode && "200" == t.data.status ? (r = "../poster/poster?posterImageUrl=" + c, 
                wx.navigateTo({
                    url: r
                })) : console.log(t);
            }).catch(function(t) {
                console.log(t);
            }).finally(function(t) {
                wx.hideLoading();
            });
        }
    }
});